package com.bootdo.lvyou_specialty.controller;

import java.awt.image.BufferedImage;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.bootdo.common.utils.*;
import com.bootdo.lvyou_specialty.domain.LvyouSpecialtyGzhConfig;
import com.bootdo.lvyou_specialty.service.LvyouSpecialtyGzhConfigService;
import com.bootdo.system.dao.UserDao;
import com.bootdo.system.domain.UserDO;
import com.bootdo.system.service.UserService;
import com.front.web.lvyou_specialty_front.GZHConfigController;
import org.apache.catalina.User;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.ui.Model;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ModelAttribute;
import com.bootdo.common.controller.BaseController;
import com.bootdo.lvyou_specialty.domain.LvyouSpecialtyMerchant;
import com.bootdo.lvyou_specialty.service.LvyouSpecialtyMerchantService;

/**
 * 
 * 
 * @author haojiangbo
 * @email 2100272728@qq.com
 * @date 2019-10-10 11:41:17
 */
 
@Controller
@RequestMapping("/lvyou_specialty/lvyouSpecialtyMerchant")
public class LvyouSpecialtyMerchantController extends BaseController{

	@Autowired
	private LvyouSpecialtyMerchantService lvyouSpecialtyMerchantService;

	@Autowired
	private UserService userService;

	@Autowired
	private LvyouSpecialtyGzhConfigService lvyouSpecialtyGzhConfigService;
	
	@ModelAttribute
	public LvyouSpecialtyMerchant get(@RequestParam(required=false) Long id) {
		if (id != null){
			System.out.println("111");
			return lvyouSpecialtyMerchantService.get(id);
		}else{
			return new LvyouSpecialtyMerchant();
		}
	}


	
	//insert into sys_menu(parent_id,name,url,perms,type)  values (149,'测试','/lvyou_specialty/lvyouSpecialtyMerchant','lvyou_specialty:lvyouSpecialtyMerchant:lvyouSpecialtyMerchant',1)
	//insert into sys_menu(parent_id,name,url,perms,type)  values (160,'修改','','lvyou_specialty:lvyouSpecialtyMerchant:edit',2)
	//type 0是分类菜单 1是小菜但 2是按钮
	@GetMapping()
	@RequiresPermissions("lvyou_specialty:lvyouSpecialtyMerchant:lvyouSpecialtyMerchant")	
	String LvyouSpecialtyMerchant(LvyouSpecialtyMerchant lvyouSpecialtyMerchant,HttpServletRequest request,HttpServletResponse response){
	    return "lvyou_specialty/lvyouSpecialtyMerchant/lvyouSpecialtyMerchant";
	}
	
	@ResponseBody
	@PostMapping("/list")
	@RequiresPermissions("lvyou_specialty:lvyouSpecialtyMerchant:lvyouSpecialtyMerchant")
	public R list(@RequestParam Map<String, Object> params){
		//查询列表数据
        Query query = new Query(params);
		List<LvyouSpecialtyMerchant> lvyouSpecialtyMerchantList = lvyouSpecialtyMerchantService.list(query);
		int total = lvyouSpecialtyMerchantService.count(query);
		PageUtils pageUtils = new PageUtils(lvyouSpecialtyMerchantList, total);
		return R.ok().put("rows", lvyouSpecialtyMerchantList).put("total", total);
	}
	
	@GetMapping("/add")
	@RequiresPermissions("lvyou_specialty:lvyouSpecialtyMerchant:edit")
	String add(LvyouSpecialtyMerchant lvyouSpecialtyMerchant,HttpServletRequest request,HttpServletResponse response){
	    return "lvyou_specialty/lvyouSpecialtyMerchant/lvyouSpecialtyMerchantedit";
	}

	@PostMapping("/getById")
	@ResponseBody
	R getById(LvyouSpecialtyMerchant lvyouSpecialtyMerchant) {
		return R.ok().put("i",lvyouSpecialtyMerchant);
	}
	
	/**
	 * 保存
	 */
	@ResponseBody
	@PostMapping("/save")
	@RequiresPermissions("lvyou_specialty:lvyouSpecialtyMerchant:edit")
	public R save( LvyouSpecialtyMerchant lvyouSpecialtyMerchant){

		lvyouSpecialtyMerchant.setDescInfo(DocumentUtil.setWidth(lvyouSpecialtyMerchant.getDescInfo()));
		if(lvyouSpecialtyMerchantService.save(lvyouSpecialtyMerchant)>0){
			return R.ok();
		}
		return R.error();
	}
	
	
	/**
	 * 删除
	 */
	@PostMapping( "/remove")
	@ResponseBody
	@RequiresPermissions("lvyou_specialty:lvyouSpecialtyMerchant:edit")
	public R remove( Long id){
		if(lvyouSpecialtyMerchantService.remove(id)>0){
		return R.ok();
		}
		return R.error();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/batchRemove")
	@ResponseBody
	@RequiresPermissions("lvyou_specialty:lvyouSpecialtyMerchant:edit")
	public R remove(@RequestParam("ids[]") Long[] ids){
		lvyouSpecialtyMerchantService.batchRemove(ids);
		return R.ok();
	}

	@PostMapping( "/addAccount")
	@ResponseBody
	@RequiresPermissions("lvyou_specialty:lvyouSpecialtyMerchant:edit")
	public R addAccount(UserDO userDO, @RequestParam(value = "merchantId")Long merchantId){

		Map<String,Object> param = new HashMap<>();
		param.put("merchantId", merchantId);
		List<UserDO> list = userService.list(param);
		if (list.size() > 0) {
			return R.error("用户已经有账户了");
		}

		if (lvyouSpecialtyMerchantService.updateUser(userDO, merchantId) == 0) {
			return R.error("保存失败");
		}

		return R.ok().put("i", lvyouSpecialtyMerchantService.get(merchantId));
	}


	@PostMapping( "/deleteAccount")
	@ResponseBody
	@RequiresPermissions("lvyou_specialty:lvyouSpecialtyMerchant:edit")
	public R deleteAccount(UserDO userDO, @RequestParam(value = "merchantId")Long merchantId){

		if (lvyouSpecialtyMerchantService.deleteUser(userDO, merchantId) == 0) {
			return R.error();
		}

		return R.ok().put("i", lvyouSpecialtyMerchantService.get(merchantId));
	}

	/**
	 * http://192.168.0.239/lvyou_specialty/lvyouSpecialtyMerchant/getUserImageNew
	 * @param response
	 * @throws Exception
	 */
	@RequestMapping("/getUserImageNew/{usermMerchantId}")
	public void getOrderQrcode(HttpServletResponse response,
							   @PathVariable("usermMerchantId")Long usermMerchantId) throws Exception {

//		// 得到用户信息
//		UserDO user = ShiroUtils.getUser();
//		if (user == null) {
//			return;
//		}
//		UserDO userDO = userService.get(ShiroUtils.getUserId());
//		if (userDO == null) {
//			return;
//		}

		// 得到公众号的配置信息

		LvyouSpecialtyGzhConfig aDefault = lvyouSpecialtyGzhConfigService.getDefault();
		String redirectUrl = URLEncoder.encode(aDefault.getRedirectUrl(), "utf-8");
		String wxUrl = "https://open.weixin.qq.com/connect/oauth2/authorize?appid="+aDefault.getAppId()+"&redirect_uri="+ redirectUrl +"&response_type=code&scope=snsapi_base&state=" + usermMerchantId + "#wechat_redirect";


		System.out.println(wxUrl);
		BufferedImage qr = QrcodeUtils.getBarCodeImgByUrl(wxUrl,640,640);
		response.setHeader("Pragma", "no-cache");
		response.setHeader("Cache-Control", "no-cache");
		response.setDateHeader("Expires", 0);
		response.setContentType("image/jpeg");
		ImageIO.write(qr, "jpg", response.getOutputStream());
		response.getOutputStream().flush();
		response.getOutputStream().close();
	}

	/**
	 * /lvyou_specialty/lvyouSpecialtyMerchant/changePass
	 * @return
	 */
	@RequestMapping("changePass")
	@RequiresPermissions("lvyou_specialty:lvyouSpecialtyMerchant:changePass")
	@ResponseBody
	public R changePass(@RequestParam("password")String password) {
		UserDO userDO = userService.getMyOwn(ShiroUtils.getUserId());
		if (userDO == null) {
			return R.error("人员参数错误");
		}
		userDO.setPassword(MD5Utils.encrypt(userDO.getUsername(), password));

		if (userService.lvyouResetPass(userDO) == 0) {
			return R.error("修改失败");
		}
		return R.ok();
	}

	/**
	 * /lvyou_specialty/lvyouSpecialtyMerchant/getUserInfo
	 * @return
	 */
	@RequestMapping("getUserInfo")
	@ResponseBody
	@RequiresPermissions("lvyou_specialty:lvyouSpecialtyMerchant:changePass")
	public PageUtils getUserInfo() {
		Map<String, Object> param = new HashMap<>();
		param.put("userId", ShiroUtils.getUserId());
		List<UserDO> list = userService.list(param);
		int total = 1;
		PageUtils pageUtil = new PageUtils(list, total);
		return pageUtil;
	}

}